<template>
  <div class="cart-content">
    <div class="header">购物清单</div>
    <div class="cart-header">
      <div class="title">商品信息</div>
      <div class="price">单价</div>
      <div class="count">数量</div>
      <div class="cost">小计</div>
      <div class="delete">删除</div>
    </div>
    <div class="cart-body">
      <div class="cart-content-main" v-for="(item,index) in cartList" :key="item.id">
        <div class="title">
          <img :src="productDictList[item.id].image"/>
          <span>{{productDictList[item.id].name}}</span>
        </div>
        <div class="price">￥{{productDictList[item.id].cost}}</div>
        <div class="count">
          <span class="cart-control-minus" @click="handleCount(index, -1)">-</span>
          {{item.count}}
          <span class="cart-control-add" @click="handleCount(index, 1)">+</span>  
        </div>
        <div class="cost">￥{{productDictList[item.id].cost * item.count}}</div>
        <div class="delete">删除</div>
      </div>
      <div class="cart-footer" v-if="cartList.length">
        <div class="cart-footer-desc">共计<span> {{count}} </span>件商品</div>
        <div class="cart-footer-desc">应付总额<span> ￥{{totalFund}} </span></div>
        <div class="cart-footer-desc">
            <div class="cart-control-order" @click="handleOrder">现在结算</div>
        </div>
      </div>
      <div class="cart-empty" v-show="cartList.length == 0" >
        购物车为空
      </div>
    </div>
    
  </div>
</template>
<script>
export default {
  data(){
    return{
      list:[],
      productList: this.$store.state.cart.productList,
    }
  },
  methods: {
    handleCount(index, count){
      if (count < 0 && this.cartList[index].count === 1) return;
      this.$store.commit('editCartCount', {id: this.cartList[index].id,count});
    },
    handleOrder(){
      this.$store.dispatch('buy').then(() => {
          window.alert('购买成功');
      })
    }
  },
  computed: {
    cartList(){
      return this.$store.state.cart.cartList;
    },
    count(){
      let count = 0;
      this.cartList.forEach(item => {
        count += item.count;
      })
      return count;
    },
    totalFund(){
      let totalFund = 0;
      this.cartList.forEach(item => {
        totalFund += item.count * this.productDictList[item.id].cost;
      })
      return totalFund;
    },
    productDictList(){
      const dict = {};
      this.productList.forEach(item => {
        dict[item.id] = item;
      });
      return dict;
    }
  }
}
</script>
<style scoped>
.cart-footer{
  padding: 32px;
  text-align: right;
  background-color: #fff;
}
.cart-footer-desc{
  display: inline-block;
  padding: 0 16px;
}
.cart-footer-desc span{
  color:#f2352e;
  font-size:20px;
}
.cart-footer-desc .cart-control-order{
  display: inline-block;
  padding: 8px 32px;
  border-radius: 6px;
  background: #2d8cf0;
  color: #fff;
  cursor: pointer;
}
.total{
  float: right;
}
.cart-control-minus, .cart-control-add{
  display: inline-block;
  margin: 0 4px;
  width: 24px;
  height: 24px;
  line-height: 22px;
  text-align: center;
  background: #f8f8f9;
  border-radius: 50%;
  box-shadow: 0 1px 1px rgba(0,0,0,.2);
  cursor: pointer;
}
.cart-body{
  border-bottom-left-radius: 10px;
  border-bottom-right-radius: 10px;
}
.cart-empty{
  padding: 32px;
  text-align: center;
  background-color: white;
}
.cart-header{
  background-color: #eee;
  padding: 8px 32px;
  border-bottom: 1px solid #dddee1;
  overflow: hidden;
}
.cart-header .title,.cart-content-main .title{
  text-align: left;
  display: inline-block;
  width: 60%;
  float: left;
  font-size: 14px;
}
.cart-content-main{
  line-height: 60px;
  padding: 0 32px;
  border-bottom: 1px dashed #e9eaec;
  overflow: hidden;
  background-color: #fff;
}
.cart-content-main img{
  width: 40px;
  height: 40px;
  position: relative;
  top: 10px;
}
.cart-header .price, .cart-header .count, .cart-header .cost, .cart-header .delete,
.cart-content-main .price, .cart-content-main .count, .cart-content-main .cost, .cart-content-main .delete {
  text-align: center;
  display: inline-block;
  width: 10%;
  float: left;
  font-size: 14px;
}

/* .cart-header .price{
  width: 10%;
}
.cart-header .count{
  width: 10%;
}
.cart-header .cost{
  width: 10%;
}
.cart-header .delete{
  width: 10%;
} */
.cart-content{
  margin: 32px;
  border:1px solid #dddee1;
  border-radius: 10px;
}
.header{
  text-align: left;
  padding:15px 30px;
  border-bottom: 1px solid #dddee1;
  border-radius: 10px 10px 0 0;
}
</style>


